home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Interapplication Comm / SmallDaemon / cSmallDaemon.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.1 KB  |  144 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  cSmallDaemon
  3.  *
  4.  *  7/92 Greg Robbins         based on code by C.K. Haun
  5.  *
  6.  *  This is a minimal faceless background application for System 7.
  7.  *
  8.  *  It demonstrates how to install and dispatch Apple events, as well
  9.  *  as the other bare essentials for a faceless background app.
  10.  *
  11.  *  The file type for this application should be 'APPL' if it will be launched
  12.  *  like an application or 'appe' if it will be placed into the Extensions
  13.  *  folder and launched at startup.  'appe' files can also have an INIT resource 
  14.  *  to put up an icon (using ShowInit) at startup.
  15.  */
  16.  
  17. #include "AppleEvents.h"
  18. #include "GestaltEqu.h"
  19.  
  20. #define kSleepMax 216000  // long sleep time (in ticks) to avoid stealing cycles
  21.                           // an app which does something on null events might
  22.                           // sleep less
  23.  
  24. Boolean gAppleEventsFlag, gQuitFlag;
  25. long gSleepVal;
  26.  
  27. #ifdef powerc
  28.    QDGlobals    qd;
  29. #endif
  30.  
  31.  
  32. // Apple event handlers to be installed
  33.  
  34. pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
  35.                                  AppleEvent * replyAppleEvent, 
  36.                                  long refCon)
  37. {
  38. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  39.     return noErr;
  40. }
  41.  
  42. pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
  43.                                AppleEvent * replyAppleEvent, 
  44.                                long refCon)
  45. {
  46. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  47.     return errAEEventNotHandled;
  48. }
  49.  
  50. pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
  51.                                 AppleEvent * replyAppleEvent, 
  52.                                 long refCon)
  53. {
  54. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  55.     return errAEEventNotHandled;
  56. }
  57.  
  58. pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
  59.                                  AppleEvent * replyAppleEvent, 
  60.                                  long refCon)
  61. {
  62. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  63.     gQuitFlag = true;
  64.     return noErr;
  65. }
  66.  
  67. void InitAppleEventsStuff(void)
  68. // install Apple event handlers
  69. {
  70.     OSErr retCode;
  71.     
  72.     if (gAppleEventsFlag) {
  73.         
  74.         retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  75.                     NewAEEventHandlerProc (DoAEOpenApplication), 0, false);
  76.                                         
  77.         if (retCode == noErr)
  78.             retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  79.                     NewAEEventHandlerProc (DoAEOpenDocuments), 0, false);
  80.  
  81.         if (retCode == noErr)
  82.             retCode = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  83.                     NewAEEventHandlerProc (DoAEPrintDocuments), 0, false);
  84.         if (retCode == noErr)
  85.             retCode = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  86.                     NewAEEventHandlerProc (DoAEQuitApplication), 0, false);
  87.         
  88.         if (retCode != noErr) DebugStr("\pInstall event handler failed");
  89.         // a better way to indicate an error is to post a notification                    
  90.     }
  91. }
  92.  
  93. void DoHighLevelEvent(EventRecord * theEventRecPtr)
  94. // high-level event dispatching
  95. {
  96.     (void) AEProcessAppleEvent(theEventRecPtr);
  97. }
  98.  
  99.  
  100. main()
  101. {
  102.     OSErr retCode;
  103.     long gestResponse;
  104.     
  105.     EventRecord mainEventRec;
  106.     Boolean eventFlag;
  107.     
  108.     // faceless background apps only get a 2K stack by default.  If necessary,
  109.     // increase the stack size here (by calling GetApplLimit to find the current
  110.     // heap limit, and SetApplLimit to set it to a lower address, thus reserving
  111.     // more space for the stack)
  112.     
  113.     // initialize QuickDraw globals
  114.     
  115.     InitGraf(&qd.thePort);
  116.     
  117.     // initialize application globals
  118.     
  119.     gQuitFlag = false;
  120.     gSleepVal = kSleepMax;
  121.     
  122.     // is the Apple Event Manager available?
  123.     retCode = Gestalt(gestaltAppleEventsAttr, &gestResponse);
  124.     if (retCode == noErr &&
  125.         (gestResponse & (1 << gestaltAppleEventsPresent)) != 0)
  126.         gAppleEventsFlag = true;
  127.     else gAppleEventsFlag = false;
  128.  
  129.     // install Apple event handlers
  130.     InitAppleEventsStuff();
  131.     
  132.     // main event loop
  133.     
  134.     while (!gQuitFlag) {
  135.         eventFlag = WaitNextEvent(everyEvent, &mainEventRec, gSleepVal, nil);
  136.         
  137.         if (mainEventRec.what == kHighLevelEvent)
  138.             DoHighLevelEvent(&mainEventRec);
  139.             
  140.         // during testing, I like to call GetKeys here and check if the CapsLock
  141.         // key is down.  If it is, set gQuitFlag so the program will exit.
  142.     }
  143. }
  144.